/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.core.modal.grr;

import com.google.common.collect.Lists;
import com.intelligent.ispc.client.ex.dynamictable.DyAbstractTableModel;
import com.intelligent.ispc.client.utils.SystemConfigUtils;
import com.intelligent.ispc.client.utils.UIConstant;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.core.dto.GrrViewDataDto;
import com.intelligent.ispc.core.dto.KeyValueDto;

import java.util.List;

/**
 * Created by cherry on 2016/7/22.
 */
public class ViewDataTableModel extends DyAbstractTableModel<GrrViewDataDto> {
    private String[] columns;
    private List<GrrViewDataDto> data;
    private int digGrrNum = UIConstant.DEFAULT_GRR_DIGNUM;

    /**
     * ViewDataTableModel constructor
     */
    public ViewDataTableModel() {
        data = Lists.newArrayList();
        digGrrNum = SystemConfigUtils.getInstance().getGrrDigit();
    }

    /**
     * viewDataTableModel constructor
     *
     * @param columns viewDataTable columns
     */
    public ViewDataTableModel(String[] columns) {
        if (columns != null) {
            this.columns = new String[columns.length];
            System.arraycopy(columns, 0, this.columns, 0, columns.length);
        }
        data = Lists.newArrayList();
    }

    @Override
    public int getRowCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getColumnCount() {
        return columns == null ? 0 : columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return (columns == null) || (columns.length <= column) ? "" : columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data != null && !data.isEmpty()) {
            GrrViewDataDto grrViewDataDto = data.get(rowIndex);
            List<KeyValueDto> keyValueDtos = Lists.newArrayList();
            if (grrViewDataDto != null) {
                keyValueDtos = grrViewDataDto.getColValueDtoList();
            }

            if (columnIndex >= 5) {
                return setItemsColumValue(keyValueDtos, columnIndex);
            }
        }
        return "-";
    }

    /**
     * set viewDataTableModel data source
     *
     * @param data viewDataTableModel data source
     */
    public void setData(List<GrrViewDataDto> data) {
        if (data == null) {
            this.data = Lists.newArrayList();
        } else if (data.isEmpty()) {
            return;
        } else {
            this.data.clear();
            this.data.addAll(data);
        }
        fireTableStructureChanged();
    }


    private Object setItemsColumValue(List<KeyValueDto> keyValueDtos, int columnIndex) {
        if (columnIndex >= columns.length) {
            return "-";
        }
        String returnValue = "";
        if (keyValueDtos != null) {
            for (int i = 0; i < keyValueDtos.size(); i++) {
                if (columns[columnIndex].equals(keyValueDtos.get(i).getKey())) {
                    returnValue = keyValueDtos.get(i).getValue().toString();
                    break;
                }
            }
        }

        if (StringUtils.isNumeric(returnValue)) {
            returnValue = StringUtils.formatDouble(Double.valueOf(returnValue), digGrrNum) + "";
        }
        return returnValue;
    }

    public List<GrrViewDataDto> getData() {
        return data;
    }

    /**
     * @param columns viewData table's columns name
     */
    public void setColumns(String[] columns) {
        if (columns == null) {
            this.columns = new String[0];
            return;
        }
        this.columns = new String[columns.length];
        System.arraycopy(columns, 0, this.columns, 0, columns.length);
    }

    /**
     * init digNum value
     */
    public void initDigGrrNum() {
        digGrrNum = SystemConfigUtils.getInstance().getGrrDigit();
    }
}
